﻿namespace Sidvall.LogManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class ServerConnection : global::Sidvall.LogManager.UI.Entities.Core.BusinessBase, global::Sidvall.LogManager.Data.IServerConnection
    {
        #region Business Members

        #region ConnectionString

        private string _ConnectionString = string.Empty;
        partial void OnBeforeConnectionStringPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeConnectionStringPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterConnectionStringPropertyChanged();
        public string ConnectionString
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_ConnectionString);
                OnBeforeConnectionStringPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _ConnectionString);
                OnBeforeConnectionStringPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ConnectionString", eventData.Value, true, true, true);
                    OnAfterConnectionStringPropertyChanged();
                }
            }
        }

        #endregion
        #region ConnectionTypeId

        private global::Sidvall.LogManager.Data.ConnectionType _ConnectionTypeId = global::Sidvall.LogManager.Data.ConnectionType.NetworkSqlServer;
        partial void OnBeforeConnectionTypeIdPropertyGet(Sidvall.Business.PropertyEventData<global::Sidvall.LogManager.Data.ConnectionType> eventData);
        partial void OnBeforeConnectionTypeIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<global::Sidvall.LogManager.Data.ConnectionType> eventData);
        partial void OnAfterConnectionTypeIdPropertyChanged();
        public global::Sidvall.LogManager.Data.ConnectionType ConnectionTypeId
        {
            get
            {
                Sidvall.Business.PropertyEventData<global::Sidvall.LogManager.Data.ConnectionType> eventData = new Sidvall.Business.PropertyEventData<global::Sidvall.LogManager.Data.ConnectionType>(_ConnectionTypeId);
                OnBeforeConnectionTypeIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<global::Sidvall.LogManager.Data.ConnectionType> eventData = new Sidvall.Business.PropertyChangeEventData<global::Sidvall.LogManager.Data.ConnectionType>(value, _ConnectionTypeId);
                OnBeforeConnectionTypeIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ConnectionTypeId", eventData.Value, true, true, true);
                    OnAfterConnectionTypeIdPropertyChanged();
                }
            }
        }

        #endregion
        #region IsEnabled

        private bool _IsEnabled = false;
        partial void OnBeforeIsEnabledPropertyGet(Sidvall.Business.PropertyEventData<bool> eventData);
        partial void OnBeforeIsEnabledPropertyChanged(Sidvall.Business.PropertyChangeEventData<bool> eventData);
        partial void OnAfterIsEnabledPropertyChanged();
        public bool IsEnabled
        {
            get
            {
                Sidvall.Business.PropertyEventData<bool> eventData = new Sidvall.Business.PropertyEventData<bool>(_IsEnabled);
                OnBeforeIsEnabledPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<bool> eventData = new Sidvall.Business.PropertyChangeEventData<bool>(value, _IsEnabled);
                OnBeforeIsEnabledPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("IsEnabled", eventData.Value, true, true, true);
                    OnAfterIsEnabledPropertyChanged();
                }
            }
        }

        #endregion
        #region IsLoaded

        private bool _IsLoaded = false;
        partial void OnBeforeIsLoadedPropertyGet(Sidvall.Business.PropertyEventData<bool> eventData);
        partial void OnBeforeIsLoadedPropertyChanged(Sidvall.Business.PropertyChangeEventData<bool> eventData);
        partial void OnAfterIsLoadedPropertyChanged();
        public bool IsLoaded
        {
            get
            {
                Sidvall.Business.PropertyEventData<bool> eventData = new Sidvall.Business.PropertyEventData<bool>(_IsLoaded);
                OnBeforeIsLoadedPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<bool> eventData = new Sidvall.Business.PropertyChangeEventData<bool>(value, _IsLoaded);
                OnBeforeIsLoadedPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("IsLoaded", eventData.Value, true, true, true);
                    OnAfterIsLoadedPropertyChanged();
                }
            }
        }

        #endregion
        #region Name

        private string _Name = string.Empty;
        partial void OnBeforeNamePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeNamePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterNamePropertyChanged();
        public string Name
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Name);
                OnBeforeNamePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Name);
                OnBeforeNamePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Name", eventData.Value, true, true, true);
                    OnAfterNamePropertyChanged();
                }
            }
        }

        #endregion
        #region RemoteAddress

        private string _RemoteAddress = string.Empty;
        partial void OnBeforeRemoteAddressPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeRemoteAddressPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterRemoteAddressPropertyChanged();
        public string RemoteAddress
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_RemoteAddress);
                OnBeforeRemoteAddressPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _RemoteAddress);
                OnBeforeRemoteAddressPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("RemoteAddress", eventData.Value, true, true, true);
                    OnAfterRemoteAddressPropertyChanged();
                }
            }
        }

        #endregion
        #region ServerConnectionId

        private System.Nullable<int> _ServerConnectionId = null;
        partial void OnBeforeServerConnectionIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData);
        partial void OnBeforeServerConnectionIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData);
        partial void OnAfterServerConnectionIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<int> ServerConnectionId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<int>>(_ServerConnectionId);
                OnBeforeServerConnectionIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<int>>(value, _ServerConnectionId);
                OnBeforeServerConnectionIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ServerConnectionId", eventData.Value, true, true, true);
                    OnAfterServerConnectionIdPropertyChanged();
                }
            }
        }

        #endregion
        #region UserConfigurationId

        private int _UserConfigurationId = 0;
        partial void OnBeforeUserConfigurationIdPropertyGet(Sidvall.Business.PropertyEventData<int> eventData);
        partial void OnBeforeUserConfigurationIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<int> eventData);
        partial void OnAfterUserConfigurationIdPropertyChanged();
        public int UserConfigurationId
        {
            get
            {
                Sidvall.Business.PropertyEventData<int> eventData = new Sidvall.Business.PropertyEventData<int>(_UserConfigurationId);
                OnBeforeUserConfigurationIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<int> eventData = new Sidvall.Business.PropertyChangeEventData<int>(value, _UserConfigurationId);
                OnBeforeUserConfigurationIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("UserConfigurationId", eventData.Value, true, true, true);
                    OnAfterUserConfigurationIdPropertyChanged();
                }
            }
        }

        #endregion
        #region UserConfiguration

        private UserConfiguration _UserConfiguration;
        partial void OnBeforeUserConfigurationPropertyChanged(Sidvall.Business.PropertyChangeEventData<UserConfiguration> eventData);
        partial void OnAfterUserConfigurationPropertyChanged();
        public UserConfiguration UserConfiguration
        {
            get
            {
                return _UserConfiguration;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<UserConfiguration> eventData = new Sidvall.Business.PropertyChangeEventData<UserConfiguration>(value, _UserConfiguration);
                OnBeforeUserConfigurationPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("UserConfiguration", eventData.Value, true, true, false);
                    OnAfterUserConfigurationPropertyChanged();
                }
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowIsValid

        public override bool DataRowIsValid(Sidvall.Data.BusinessRuleFilter filter)
        {
            return global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.DataRowIsValid(this, filter);
        }

        #endregion
        #region DataRowStateIsDirty

        public override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.DataRowStateIsDirty(this);
            }
        }

        #endregion

        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public ServerConnection Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public ServerConnection Copy()
        {
            return Copy(null);
        }
        public ServerConnection Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new ServerConnection();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.LogManager.Data.IServerConnection item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.LogManager.Data.IServerConnection item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(ServerConnection destination, global::Sidvall.LogManager.Data.IServerConnection source, Sidvall.Data.CopySettings copySettings);
        private void Import(ServerConnection destination, global::Sidvall.LogManager.Data.IServerConnection source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(ServerConnection destination, ServerConnection source, Sidvall.Data.CopySettings copySettings);
        private void Import(ServerConnection destination, ServerConnection source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.LogManager.Data.IServerConnection, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.LogManager.UI.SystemContextConfig.Current.EntityFacade.ServerConnectionManager.Initialize(this, settings);
        }

        #endregion
        
        #region IServerConnection Members

        string global::Sidvall.LogManager.Data.IServerConnection.ConnectionString
        {
            get
            {
                return this._ConnectionString;
            }
            set
            {
                this._ConnectionString = value;
            }
        }
        global::Sidvall.LogManager.Data.ConnectionType global::Sidvall.LogManager.Data.IServerConnection.ConnectionTypeId
        {
            get
            {
                return this._ConnectionTypeId;
            }
            set
            {
                this._ConnectionTypeId = value;
            }
        }
        bool global::Sidvall.LogManager.Data.IServerConnection.IsEnabled
        {
            get
            {
                return this._IsEnabled;
            }
            set
            {
                this._IsEnabled = value;
            }
        }
        bool global::Sidvall.LogManager.Data.IServerConnection.IsLoaded
        {
            get
            {
                return this._IsLoaded;
            }
            set
            {
                this._IsLoaded = value;
            }
        }
        string global::Sidvall.LogManager.Data.IServerConnection.Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }
        string global::Sidvall.LogManager.Data.IServerConnection.RemoteAddress
        {
            get
            {
                return this._RemoteAddress;
            }
            set
            {
                this._RemoteAddress = value;
            }
        }
        System.Nullable<int> global::Sidvall.LogManager.Data.IServerConnection.ServerConnectionId
        {
            get
            {
                return this._ServerConnectionId;
            }
            set
            {
                this._ServerConnectionId = value;
            }
        }
        int global::Sidvall.LogManager.Data.IServerConnection.UserConfigurationId
        {
            get
            {
                return this._UserConfigurationId;
            }
            set
            {
                this._UserConfigurationId = value;
            }
        }
        global::Sidvall.LogManager.Data.IUserConfiguration global::Sidvall.LogManager.Data.IServerConnection.UserConfiguration
        {
            get
            {
                return this._UserConfiguration;
            }
            set
            {
                UpdateChildProperty(this._UserConfiguration, value);
                this._UserConfiguration = value as UserConfiguration;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public ServerConnection()
        {
        }

        public ServerConnection(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}